/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.controlers;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import lombok.Getter;
import lombok.Setter;
import etf.pia.entity.*;
import etf.pia.util.*;
import static etf.pia.util.MyLogger.log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.DualListModel;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Alejandro
 */
@Getter
@Setter
@ManagedBean(name = "adminBean")
@SessionScoped
public class AdminBean {

    private boolean noviKorisnik = true;
    private Predmet newP;
    private Obavestenja newO;
    private Obavestenja selectedO;
    private Predmet selectedP;
    private String selectedRole;
    private LabVezbe newL;
    private Domaci newD;
    private Korisnik newK;
    private Zaposleni prof;
    private Zaposleni selectedProf;
    private Student student;
    private Student selectedStudent;
    private ArrayList<Predmet> pList;
    private ArrayList<Student> studentList;
    private ArrayList<Zaposleni> profList;
    private ArrayList<Korisnik> korisnici;
    private ArrayList<Obavestenja> obavestenja;
    private ArrayList<PlanAngazovanja> planList;
    private ArrayList<Raspored> raspored;
    private ArrayList<String> typeList = new ArrayList<String>();
    private ArrayList<String> role = new ArrayList<String>();
    private String group;

    public AdminBean() {
        typeList.add("izborni");
        typeList.add("obavezni");
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Criteria c = ses.createCriteria(Predmet.class);
            pList = (ArrayList<Predmet>) c.list();
            Initializer.initKatedra(pList);
            c = ses.createCriteria(Obavestenja.class);
            c.add(Restrictions.isNull("predmet"));
            obavestenja = (ArrayList<Obavestenja>) c.list();
            selectedO = obavestenja.get(0);
            c = ses.createCriteria(Korisnik.class);
            korisnici = (ArrayList<Korisnik>) c.list();
            for (Korisnik k : korisnici) {
                Hibernate.initialize(k.getRole());
            }
            c = ses.createCriteria(Zaposleni.class);
            profList = (ArrayList<Zaposleni>) c.list();
            selectedProf = profList.get(0);
            c = ses.createCriteria(Student.class);
            studentList = (ArrayList<Student>) c.list();
            selectedStudent = studentList.get(0);
            c = ses.createCriteria(PlanAngazovanja.class);
            planList = (ArrayList<PlanAngazovanja>) c.list();
            c = ses.createCriteria(Raspored.class);
            raspored = (ArrayList<Raspored>) c.list();
            newP = new Predmet();
            newL = new LabVezbe();
            newD = new Domaci();
            newK = new Korisnik();
            newO = new Obavestenja();
            student = new Student();
            prof = new Zaposleni();
            role.add("admin");
            role.add("zaposleni");
            role.add("student");
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addCourse() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.save(newP);
            t.commit();
            pList.add(newP);
            newP = new Predmet();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addLab() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            newL.setPredmet(selectedP);
            ses.save(newL);
            t.commit();
            newL = new LabVezbe();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addDomaci() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            newD.setPredmet(selectedP);
            ses.save(newD);
            t.commit();
            newD = new Domaci();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public String edit(Predmet p) {
        selectedP = p;
        return "adminCourseEdit.xhtml?faces-redirect=true";
    }

    public void updateInfo() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            ses.update(selectedO);
            t.commit();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addInfo() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            newO.setDatum(new Date());
            ses.save(newO);
            t.commit();
            obavestenja.add(newO);
            newO = new Obavestenja();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addPlan() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            PlanAngazovanja p = new PlanAngazovanja();
            p.setGrupa(group);
            p.setPredmet(selectedP);
            p.setZaposleni(selectedProf);
            ses.save(p);
            t.commit();
            planList.add(p);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addRaspored() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Raspored p = new Raspored();
            p.setGrupa(group);
            p.setPredmet(selectedP);
            p.setStudent(selectedStudent);
            ses.save(p);
            t.commit();
            raspored.add(p);
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }

    public void addUser() {
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {

            Criteria c = ses.createCriteria(Role.class);
            c.add(Restrictions.eq("description", selectedRole));
            newK.setRole((Role) c.uniqueResult());

            if (selectedRole.equals("student")) {
                newK.setStudent(student);
                student.setKorisnik(newK);
                studentList.add(student);
                student.setTip('m');
                student.setStatus("aktivan");
                ses.save(newK);
                ses.save(student);
                student = new Student();
            } else if (selectedRole.equals("zaposleni")) {
                newK.setZaposleni(prof);
                prof.setKorisnik(newK);
                profList.add(prof);
                ses.save(newK);
                ses.save(prof);
                prof = new Zaposleni();
            } else if (selectedRole.equals("admin")) {
                ses.save(newK);
            }
            t.commit();
            korisnici.add(newK);
            newK = new Korisnik();
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
    }
}
